package com.zidian.study.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.student.trucks.R;
import com.zidian.study.adapter.EvaTagAdapter;
import com.zidian.study.bean.TagsBean;
import com.zidian.study.globa.Constant;
import com.zidian.study.globa.ToolBarActivity;
import com.zidian.study.utils.GetUserInfo;
import com.zidian.study.utils.ProgressBarBlackUtil;
import com.zidian.study.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/9/4 0004.
 * <p/>
 * 评价标签列表
 */
public class EvaluateTagActivity extends ToolBarActivity {
    @Bind(R.id.tag_list_rv)
    RecyclerView tagListRv;
    @Bind(R.id.toolbar_sumbit)
    TextView toolbarSumbit;
    @Bind(R.id.tag_commit_btn)
    Button tagCommitBtn;

    private EvaTagAdapter adapter;
    private List<TagsBean.DataBean.PushLabelBean> data;
    private List<TagsBean.DataBean.SubmitLabelBean> submitLabels;
    private String byEvaluatePersonId;
    private String twoIndexName;
    private String headPortrait;
    private String courseName;
    private String courseId;
    private int pushId;
    private String pushTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_evaluate_tags);
        setContentView(R.layout.activity_evaluate_tag_demo);
        ButterKnife.bind(this);

        toolbarSumbit.setText("教师热词");
        Intent intent = getIntent();
        byEvaluatePersonId = intent.getStringExtra("teacherId");
        twoIndexName = intent.getStringExtra("twoIndexName");
        pushId = intent.getIntExtra("pushId" , 0);
        headPortrait = intent.getStringExtra("headPortrait");
        pushTime = intent.getStringExtra("pushTime");
        courseId = intent.getStringExtra("courseId");
        courseName = intent.getStringExtra("courseName");
        setTitle(twoIndexName);
        getData();
        initView();
    }

    protected void initView() {
        data = new ArrayList<>();
        submitLabels = new ArrayList<>();
        adapter = new EvaTagAdapter();
        tagListRv.setLayoutManager(new LinearLayoutManager(this));
        tagListRv.setAdapter(adapter);

    }

    @OnClick(R.id.toolbar_sumbit)
    public void onTopClick() {
        Intent intent = new Intent(this, TagTopActivity.class);
        intent.putExtra("teacherId", byEvaluatePersonId);
        intent.putExtra("headPortrait", headPortrait);
        startActivity(intent);
    }

    /**
     * 点击提交评价
     */
    @OnClick(R.id.tag_commit_btn)
    public void onCommitClick() {
        if (submitLabels.size() > 0) {
            Snackbar.make(tagCommitBtn, "您已经评价过了", Snackbar.LENGTH_SHORT).show();
        } else {
            if (getReback() != null) {
                evaluateTag(getReback());
            }
        }
    }

    public void getData() {

        String loginUrl = Constant.URL_TEACHER + "EvaluateByStudent/pushLabelParticulars";
        HashMap<String, String> map = new HashMap<>();
        map.put("twoIndexName", twoIndexName);
        map.put("studentId", GetUserInfo.getStudentId(context));
        map.put("pushId", pushId + "");
        map.put("token", GetUserInfo.getToken(context));
        ProgressBarBlackUtil.loadShow(context);
        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.showToast(context, "获取标签列表失败！");
                ProgressBarBlackUtil.loadCancel();
            }

            @Override
            public void onResponse(String response, int id) {
                ProgressBarBlackUtil.loadCancel();
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {
                        ToastUtil.showToast(context, "获取标签列表失败！");
                        return;
                    } else {
                        Gson gson = new Gson();
                        TagsBean tags = gson.fromJson(response, TagsBean.class);
                        data.addAll(tags.getData().getPushLabel());
                        submitLabels = tags.getData().getSubmitLabel();
                        if (submitLabels.size() != 0) {
                            tagCommitBtn.setText("已评价");
                            tagCommitBtn.setEnabled(false);
                        }
                        adapter.setSubmitLabels(submitLabels);
                        adapter.setData(data);
                    }

                } catch (JSONException e) {
                    Log.e("T", e.getMessage());
                }

            }
        });

    }

    /***
     * 得到反馈字符串
     * @return
     */
    public String getReback() {
        Map<Integer, String> map = new HashMap<>();
        map = adapter.getSelected();
        StringBuilder result = new StringBuilder();

        result = result.append(map.get(0));
        if ( map.size() < data.size()) {
            Snackbar.make(tagListRv, "请完成标签选取", Snackbar.LENGTH_SHORT).show();
            return null;
        } else {
            for (int i = 1; i < data.size(); i++) {

                if (map.get(i) == null) {
                    result = null;
                    break;
                }
                result.append("," + map.get(i));
            }
        }
        return result.toString();

    }

    /**
     * 评价老师
     * @param evaluateLabel
     */
    private void evaluateTag(String evaluateLabel) {


        String loginUrl = Constant.URL_TEACHER + "EvaluateByStudent/evaluationStudent";
        HashMap<String, String> map = new HashMap<>();
        map.put("studentId", GetUserInfo.getStudentId(context));
        map.put("byEvaluatePersonId", byEvaluatePersonId);
        map.put("evaluateLabel", evaluateLabel);
        map.put("pushId", pushId + "");
        map.put("pushTime",  pushTime);
        map.put("courseId",courseId);
        map.put("courseName",courseName);
        map.put("token", GetUserInfo.getToken(context));

        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("E", e.toString());
            }

            @Override
            public void onResponse(String response, int id) {

                Log.e("T", response);
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {
                        ToastUtil.showToast(context, "评价失败！");
                        return;
                    } else {
                        ToastUtil.showToast(context, jsonObject.getString("integral"));
                        finish();
                    }

                } catch (JSONException e) {

                }


            }
        });

    }


}
